package component.fulltextsearch.index.handlingtypes;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.rtf.RTFEditorKit;


public class JavaBuiltInRTFHandler implements IInputStreamHandler {

	public String getText(InputStream is) throws InputStreamHandlerException {

		String bodyText = null;

		DefaultStyledDocument styledDoc = new DefaultStyledDocument();
		try {
			new RTFEditorKit().read(is, styledDoc, 0);
			bodyText = styledDoc.getText(0, styledDoc.getLength());
		} catch (IOException e) {
			throw new InputStreamHandlerException(
					"Cannot extract text from a RTF document", e);
		} catch (BadLocationException e) {
			throw new InputStreamHandlerException(
					"Cannot extract text from a RTF document", e);
		}

		return bodyText;
	}

	public static void main(String[] args) throws Exception {
		JavaBuiltInRTFHandler handler = new JavaBuiltInRTFHandler();
		String doc = handler.getText(new FileInputStream(new File(args[0])));
		System.out.println(doc);
	}
}
